package com.easpeed.comon.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.os.Bundle

/**
 * Created by bigyu on 2024/11/6 17:45
 * com.easpeed.comon.utils
 */
@SuppressLint("StaticFieldLeak")
object ActivityLifecycleHelper {


    private var currentActivity: Activity? = null

    @JvmStatic
    fun initLifeline(app: Application) {
        // 注册 ActivityLifecycleCallbacks
        app.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityResumed(activity: Activity) {
                currentActivity = activity
            }

            override fun onActivityPaused(activity: Activity) {
                if (currentActivity == activity) {
                    currentActivity = null
                }
            }

            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
            override fun onActivityStarted(activity: Activity) {}
            override fun onActivityDestroyed(activity: Activity) {
                if (currentActivity == activity) {
                    currentActivity = null
                }
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
            override fun onActivityStopped(activity: Activity) {}
        })
    }

    @JvmStatic
     fun getCurrentActivity(): Activity? {
        return currentActivity
    }
}